#!/bin/bash
# chkconfig:2345 30 70
# description: Mongo database
# precessname: mongod

source /etc/profile

mongodb_cluster_home="/opt/mongodb_cluster"

config_configfile="$mongodb_cluster_home/conf/config.conf"
mongos_configfile="$mongodb_cluster_home/conf/mongos.conf"
shard1_configfile="$mongodb_cluster_home/conf/shard_server1.conf"
shard2_configfile="$mongodb_cluster_home/conf/shard_server2.conf"
shard3_configfile="$mongodb_cluster_home/conf/shard_server3.conf"

config_datadir="$mongodb_cluster_home/config_server/data"
shard1_datadir="$mongodb_cluster_home/shard_server1/data"
shard2_datadir="$mongodb_cluster_home/shard_server2/data"
shard3_datadir="$mongodb_cluster_home/shard_server3/data"

config_pidfile="$mongodb_cluster_home/config_server/logs/configsvr.pid"
mongos_pidfile="$mongodb_cluster_home/mongos/logs/mongos.pid"
shard1_pidfile="$mongodb_cluster_home/shard_server1/logs/shard1.pid"
shard2_pidfile="$mongodb_cluster_home/shard_server2/logs/shard2.pid"
shard3_pidfile="$mongodb_cluster_home/shard_server3/logs/shard3.pid"

function start-config(){
    #print the tips
    echo -n $"Starting mongod of config_server:"
    su - mongodb -c "mongod -f $config_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        touch $config_datadir/mongod.lock
    fi 
}

function stop-config(){
    #print the tips
    echo  $"stopping mongod of config_server:"
    su - mongodb -c "mongod --shutdown --dbpath $config_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $config_datadir/mongod.lock
        rm -f $config_pidfile
    fi 
}

function start-mongos(){
    #print the tips
    echo  $"Starting mongod of mongos_server:"
    su - mongodb -c "mongos -f $mongos_configfile"
}

function stop-mongos(){
    echo  $"stopping mongod of mongos_server:"
    kill `cat $mongos_pidfile`
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $mongos_pidfile
    fi 
}

function start-shard1(){
    #print the tips
    echo  $"Starting mongod of shard1_server:"
    su - mongodb -c "mongod -f $shard1_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard1_datadir/mongod.lock
    fi
}

function stop-shard1(){
    echo  $"stopping mongod of shard1_server:"
    su - mongodb -c "mongod --shutdown --dbpath $shard1_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard1_datadir/mongod.lock
        rm -f $shard1_pidfile
    fi
}

function start-shard2(){
    #print the tips
    echo  $"Starting mongod of shard2_server:"
    su - mongodb -c "mongod -f $shard2_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard2_datadir/mongod.lock
    fi
}

function stop-shard2(){
    echo  $"stopping mongod of shard2_server:"
    su - mongodb -c "mongod --shutdown --dbpath $shard2_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard2_datadir/mongod.lock
        rm -f $shard2_pidfile
    fi
}

function start-shard3(){
    #print the tips
    echo  $"Starting mongod of shard3_server:"
    su - mongodb -c "mongod -f $shard3_configfile"
    #get the result
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        $shard3_datadir/mongod.lock
    fi
}

function stop-shard3(){
    echo  $"stopping mongod of shard3_server:"
    su - mongodb -c "mongod --shutdown --dbpath $shard3_datadir"
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
        rm -f $shard3_datadir/mongod.lock
        rm -f $shard3_pidfile
    fi
}
RETVAL=0
case "$1" in
    start)
      start-config
      start-shard1
      start-shard2
      start-shard3
      start-mongos
      ;;
    stop)
      stop-mongos
      stop-shard1
      stop-shard2
      stop-shard3
      stop-config
      ;;
    restart)
      $0 stop
      $0 start 
      ;;
esac
exit $RETVAL
